package com.supconit.video.controller;

import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.supconit.video.utils.AjaxMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description：监控视频
 * @author: qinling
 * @Date: 2021-02-22 15:27
 */
@Slf4j
@RestController
@RequestMapping("/monitor")

public class MonitorController {


    @Value("${hk.host}")
    private String host;

    @Value("${hk.appKey}")
    private String appKey;

    @Value("${hk.appSecret}")
    private String appSecret;

    @Value("${hk.uri}")
    private String uri;

    @Value("${hk.tokenUrl}")
    private String tokenUrl;

    @Value("${hk.http}")
    private String http;

    @Value("${hk.language}")
    private String language;

    @Value("${hk.userCode}")
    private String userCode;

    @GetMapping("/previewURL")
    public AjaxMessage GetCameraPreviewURL(@RequestParam String cameraCode) {
        try {

            //根据物联网设备id查出摄像头唯一编码


            /**
             * 设置平台参数，根据实际情况,设置host appkey appsecret 三个参数.
             * artemis网关服务器ip端口
             * 秘钥appkey
             * 秘钥appSecret
             */
            ArtemisConfig.host = host;
            ArtemisConfig.appKey = appKey;
            ArtemisConfig.appSecret = appSecret;
            /**
             * 设置接口的URI地址
             * 根据现场环境部署确认是http还是https
             */
            Map<String, String> path = new HashMap<String, String>(2) {
                {
                    put(http, uri);
                }
            };
            /**
             * 设置参数提交方式
             */
            String contentType = "application/json";

            /**
             * 组装请求参数
             */
            JSONObject jsonBody = new JSONObject();
            jsonBody.put("cameraIndexCode", cameraCode);
            jsonBody.put("streamType", 0);
            jsonBody.put("protocol", "hls");
            jsonBody.put("transmode", 1);
            jsonBody.put("expand", "streamform=ts");
            String body = jsonBody.toJSONString();
            /**
             * 调用接口 post请求application/json类型参数
             */
            String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType, null);
            if (result != null) {
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (jsonObject.getString("code").equals("0")) {
                    return AjaxMessage.success().data(jsonObject.getJSONObject("data").getString("url"));
                } else {
                    return AjaxMessage.error(jsonObject.getString("msg"));
                }
            } else {
                return AjaxMessage.error("请检查视频地址连接");
            }
        } catch (Exception e) {
            log.info("GetCameraPreviewURL:" + e.getMessage());
            return AjaxMessage.error("获取视频监控地址异常！");
        }
    }

    @GetMapping("/getHkToken")
    public AjaxMessage getHkToken() {
        try {
            ArtemisConfig.host = host;
            ArtemisConfig.appKey = appKey;
            ArtemisConfig.appSecret = appSecret;
            Map<String, String> path = new HashMap<String, String>(2) {
                {
                    put(http, tokenUrl);
                }
            };

            Map<String, String> querys = new HashMap<String, String>(3) {
                {
                    put("language", language);
                    put("service", http + host);
                    put("userCode", userCode);
                }
            };
            String result = ArtemisHttpUtil.doGetArtemis(path, querys, null,
                    "application/json");
            return AjaxMessage.success().data(JSONObject.parse(result));


        } catch (Exception e) {
            log.info("getHkToken:" + e.getMessage());
            return AjaxMessage.error("获取海康的token异常！");
        }
    }


}
